<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui-soul-table</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="//www.layuicdn.com/layui-v2.5.5/css/layui.css" media="all"/>
    <style>
        .unshow>#result {
            display: none;
        }
        pre { padding: 5px; margin: 5px; }
        .string { color: green; }
        .number { color: darkorange; }
        .boolean { color: blue; }
        .null { color: magenta; }
        .key { color: red; }
        blockquote {
            padding: 20px;
            margin: 1em 0;
            background-color: #f7f7f7;
            border-left: 0.25em solid #b4b4b4;
            word-break: break-word;
            font-size: 16px;
            font-weight: 400;
            line-height: 30px;
        }
    </style>
</head>
<body>
<blockquote>通过 <code>isChild: function(row){return row.dynasty === '唐代'}</code>控制只有朝代为唐代的会有子表展示入口</blockquote>
<div class="layui-row">
    <div class="layui-col-lg3 layui-col-md4 layui-col-sm6">
        <div class="layui-inline">
            <label class="layui-form-label">诗词</label>
            <div class="layui-input-inline" >
                <input type="text" name="title"
                       class="layui-input" placeholder="请输入诗词关键字">
            </div>
        </div>
    </div>
    <div class="layui-col-lg3 layui-col-md4 layui-col-sm6">
        <div class="layui-inline">
            <label class="layui-form-label">内容</label>
            <div class="layui-input-inline">
                <input type="text" name="content"
                       class="layui-input" placeholder="请选择内容关键词">
            </div>
        </div>
    </div>
    <div class="layui-col-lg1 layui-col-md1 layui-col-sm2 layui-col-xs3">
        <button class="layui-btn mgl-20" lay-submit="" lay-filter="search"><i class="layui-icon">&#xe615;</i>查询</button>
    </div>
    <div class="layui-col-lg1 layui-col-md1 layui-col-sm2 layui-col-xs3">
        <button class="layui-btn mgl-20" lay-submit="" lay-filter="export"><i class="layui-icon">&#xe615;</i>导出</button>
    </div>
</div>
<table id="myTable" lay-filter="myTable"></table>
<script type="text/html" id="bar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="childBar">
    <a class="layui-btn layui-btn-xs" lay-event="childEdit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="childDel">删除</a>
</script>
<div class="unshow">
    <pre id="result">

    </pre>
</div>
<script type="text/javascript" src="//www.layuicdn.com/layui/layui.js"></script>
<script>
    // 自定义模块
    layui.config({
        base: 'ext/',   // 模块目录
        version: 'v1.3.28'
    }).extend({                         // 模块别名
        soulTable: 'tableChild'
    });

    layui.use(['form', 'table','tableChild'], function () {
        var table = layui.table,
            form = layui.form,
            tableChild = layui.tableChild,
            $ = layui.$;

        var myTable = table.render({
            elem: '#myTable'
            ,height: $(document).height() - $('#myTable').offset().top - 20
            ,limit: 30
            ,soulSort: true
            ,page: true
            ,cols: [[
                {title: '#', width: 50, isChild: function(row){return row.dynasty === '唐代'}, children:[
                        {
                            title: '表格一'
                            ,url: 'data.json'
                            ,height: 300
                            ,limit: 10000000
                            ,page: false
                            ,toolEvent: function (obj, pdata) {
                                // obj 子表当前行对象
                                // pdata 父表当前行数据
                                if (obj.event === 'childEdit') {
                                    $('#result').html(syntaxHighlight(obj.data));
                                    layer.open({
                                        type: 1,
                                        title: '子表1-编辑',
                                        shadeClose: true,
                                        content:$('#result')
                                    });
                                } else if (obj.event === 'childDel') {
                                    $('#result').html(syntaxHighlight(obj.data));
                                    layer.open({
                                        type: 1,
                                        title: '子表1-删除',
                                        shadeClose: true,
                                        content:$('#result')
                                    });
                                }

                            }
                            ,rowEvent: function (obj, pdata) {
                                // 单击行事件
                                // obj 子表当前行对象
                                // pdata 父表当前行数据
                            }
                            ,rowDoubleEvent: function (obj, pdata) {
                                // 双击行事件
                                // obj 子表当前行对象
                                // pdata 父表当前行数据
                                $('#result').html('子表数据：<br>' + syntaxHighlight(obj.data)+
                                        '<br> 父表数据：<br>' +syntaxHighlight(pdata));
                                layer.open({
                                    type: 1,
                                    title: '子表1-双击行事件',
                                    shadeClose: true,
                                    content:$('#result')
                                });
                            }
                            ,cols: [[
                                {type: 'checkbox', fixed: 'left'},
                                {field: 'title', title: '诗词', width: 200, sort: true, filter: true},
                                {field: 'dynasty', title: '朝代', sort: true, filter: true},
                                {field: 'author', title: '作者', width: 165 , filter: true},
                                {field: 'type', title: '类型', width: 112,  filter: {split:','}, sort:true},
            {field: 'content', title: '内容', width: 823, filter: true},
                                {field: 'heat', title: '点赞数', width: 112,  filter: true, sort:true},
                                {field: 'createTime', title: '录入时间', width: 165, filter: {type: 'date[yyyy-MM-dd HH:mm:ss]'}, sort:true},
                                {title: '操作', width: 156, templet: '#childBar'}
                            ]]
                            ,done: function () {
                            }
                        },
                        {
                            title: '表格二'
                            ,height: 300
                            ,limit: 1000000
                            ,data: function (d) {
                                // d 为当前行数据
                                console.log(d)
                                return [d];
                            }
                            ,toolEvent: function (obj) {
                                if (obj.event === 'childEdit') {
                                    $('#result').html(syntaxHighlight(obj.data));
                                    layer.open({
                                        type: 1,
                                        title: '子表2-编辑',
                                        shadeClose: true,
                                        content:$('#result')
                                    });
                                } else if (obj.event === 'childDel') {
                                    $('#result').html(syntaxHighlight(obj.data));
                                    layer.open({
                                        type: 1,
                                        title: '子表2-删除',
                                        shadeClose: true,
                                        content:$('#result')
                                    });
                                }

                            }
                            ,page: false
                            ,cols: [[
                                {type: 'checkbox', fixed: 'left'},
                                {field: 'title', title: '诗词', width: 200, sort: true, filter: true},
                                {field: 'dynasty', title: '朝代', width: 100, sort: true, filter: true},
                                {field: 'author', title: '作者', width: 165 , filter: true},
                                {field: 'type', title: '类型', width: 112,  filter: {split:','}, sort:true},
            {field: 'content', title: '内容', width: 823, filter: true},
                                {field: 'heat', title: '点赞数', width: 112,  filter: true, sort:true},
                                {field: 'createTime', title: '录入时间', width: 165, filter: {type: 'date[yyyy-MM-dd HH:mm:ss]'}, sort:true},
                                {title: '操作', width: 156, templet: '#childBar'}
                            ]]
                            ,done: function () {
                            }
                        }
                    ]},
                {field: 'title', title: '诗词', width: 100, sort: true, filter: true},
                {field: 'dynasty', title: '朝代', sort: true, filter: true},
                {field: 'author', title: '作者', width: 165 , filter: true},
                {field: 'type', title: '类型', width: 112,  filter: {split:','}, sort:true},
            {field: 'content', title: '内容', width: 823, filter: true},
                {field: 'heat', title: '点赞数', width: 112,  filter: true, sort:true},
                {field: 'createTime', title: '录入时间', width: 165, filter: {type: 'date[yyyy-MM-dd HH:mm:ss]'}, sort:true},
                {title: '操作', width: 156, templet: '#bar'}
            ]]
            ,done: function () {
                tableChild.render(this)
            }
        });
        search({});
        function search(data) {
            var loading = layer.load(2);
            $.ajax({
                url: 'data.json',
                data: data,
                dataType: 'json',
                success: function (res) {
                    myTable.reload({
                        data: res.data
                    })
                },
                complete: function () {
                    layer.close(loading)
                }
            })
        }
        form.on('submit(search)', function (data) {
            search(data.field);
        })
        form.on('submit(export)', function () {
            soulTable.export(myTable);
        })

        table.on('tool(myTable)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            if (obj.event === 'edit') {
                $('#result').html(syntaxHighlight(obj.data));
                layer.open({
                    type: 1,
                    title: '主表-编辑',
                    shadeClose: true,
                    content:$('#result')
                });
            } else if (obj.event === 'del') {
                $('#result').html(syntaxHighlight(obj.data));
                layer.open({
                    type: 1,
                    title: '主表-删除',
                    shadeClose: true,
                    content:$('#result')
                });
            }
        })
        // json 格式化+高亮
        function syntaxHighlight(json) {
            if (typeof json != 'string') {
                json = JSON.stringify(json, undefined, 2);
            }
            json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
            return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function(match) {
                var cls = 'number';
                if (/^"/.test(match)) {
                    if (/:$/.test(match)) {
                        cls = 'key';
                    } else {
                        cls = 'string';
                    }
                } else if (/true|false/.test(match)) {
                    cls = 'boolean';
                } else if (/null/.test(match)) {
                    cls = 'null';
                }
                return '<span class="' + cls + '">' + match + '</span>';
            });
        }

    })
</script>
</body>
</html>
